/**
 * 
 */
package com.geezdata.cps.ttk.query.query.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.ttk.common.request.QuerySharerListRequest;
import com.geezdata.cps.ttk.common.response.QuerySharerListResult;
import com.geezdata.cps.ttk.common.vo.SharerResVO;
import com.geezdata.cps.ttk.domain.domain.Sharer;
import com.geezdata.cps.ttk.mapper.mapper.SharerMapper;
import com.geezdata.cps.ttk.query.query.SharerService;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;

/**
 * @author sszheng
 * @date 创建时间：2018年10月26日 下午3:01:08
 *
 */
@Service
public class SharerServiceImpl implements SharerService {
	@Autowired
	private SharerMapper sharerMapper;
	
	@Override
	public Sharer getSharerByOpenId(String openId) {
		return getSharerByOpenId(openId, Boolean.TRUE);
	}
	
	@Override
	public Sharer getSharerByOpenId(String openId, Boolean throwException ) {
		if(openId == null) {
			throw new MyException(ErrorCode.SHARER_NULL.getCode(), ErrorCode.SHARER_NULL.getDesc());
		}
		Sharer condition = new Sharer();
		condition.setOpenId(openId);
		Sharer sharer = sharerMapper.selectOne(condition);
		if (sharer != null) {
			return sharer;
		}

		if (throwException) {
			throw new MyException(ErrorCode.SHARER_NULL.getCode(), ErrorCode.SHARER_NULL.getDesc());
		}
		return null;
	}
	
	@Override
	public QuerySharerListResult getSharerList(QuerySharerListRequest request) {
		QuerySharerListResult result = new QuerySharerListResult();
		PageHelper.startPage(request.getPageNo(), request.getPageSize());
		Map<String, Object> params = Maps.newHashMap();
		params.put("nickName", request.getNickName());
		params.put("isHeader", request.getIsHeader());
		List<Sharer> sharerList = sharerMapper.selectBatch(params );
		List<SharerResVO> list = BeanUtil.convertList(sharerList, SharerResVO.class);
		result.setList(list);
		return result;
	}
}
